package heuristicFunctions;

import java.util.List;

import domain.PacmanBoard;
import aima.core.search.framework.HeuristicFunction;
import aima.core.util.datastructure.XYLocation;

public class DichtsteKruimelHeuristiek implements HeuristicFunction {

	@Override
	public double h(Object state) {
		PacmanBoard board = (PacmanBoard) state;
		XYLocation actor = board.getPacmanLocation();
		List<XYLocation> crumbs= board.getCrumbs();
		
		//vinden dichstbijzijnde kruimel
		if(crumbs.size() > 0)
		{
			XYLocation closestCrumb = crumbs.get(0);

			for (XYLocation crumb : crumbs) {
				double distanceToCurrentClosestCrumb = Math.sqrt(Math.pow(actor.getXCoOrdinate() - closestCrumb.getXCoOrdinate(),2) + Math.pow(actor.getYCoOrdinate() - closestCrumb.getYCoOrdinate(), 2));
				double distanceToCrumb = Math.sqrt(Math.pow(actor.getXCoOrdinate() - crumb.getXCoOrdinate(),2) + Math.pow(actor.getYCoOrdinate() - crumb.getYCoOrdinate(), 2));
				if(distanceToCrumb < distanceToCurrentClosestCrumb) {
					closestCrumb = crumb;
				}
			}
			
			return Math.sqrt(Math.pow(actor.getXCoOrdinate() - closestCrumb.getXCoOrdinate(),2) + Math.pow(actor.getYCoOrdinate() - closestCrumb.getYCoOrdinate(), 2));
		}
		return 0;
	}

}
